package cn.zucc.edu.madq.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.codehaus.jackson.map.util.JSONPObject;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.zucc.edu.madq.service.UserService;
import cn.zucc.edu.madq.pojo.User;

@Controller
@RequestMapping("user")
public class UserController {
	@Autowired
	private UserService userService;
	private static Logger logger = Logger.getLogger(UserController.class);

	@RequestMapping("regUser")
	@ResponseBody
	public JSONPObject regUser(
			@RequestParam("jsonpCallback") String jsonpCallback,
			HttpServletRequest request, HttpServletResponse response,
			int userId, String password) {
		logger.info("------start------" + request.getPathInfo()
				+ "------time------" + System.currentTimeMillis());
		int count = 0;
		User user;
		try {
			String sessionCode = (String) request.getSession().getAttribute(
					"code");
			logger.info("sessionCode=" + sessionCode);
			user = new User();
			user.setUserId(userId);
			user.setUserPassword(MD5Utils.MD5(password));
			count = userService.insertUser(user);
		} catch (Exception e) {
			count = -1;
			logger.error(e.getMessage(), e);
		}
		logger.info("------end------" + request.getPathInfo()
				+ "------time------" + System.currentTimeMillis());
		return new JSONPObject(jsonpCallback, count);
	}

	@RequestMapping("updatePassword")
	@ResponseBody
	public JSONPObject updatePassword(
			@RequestParam("jsonpCallback") String jsonpCallback,
			HttpServletRequest request, HttpServletResponse response,
			String newPassword,String oldPassword) {
		logger.info("------star------" + request.getPathInfo()
				+ "------time------" + System.currentTimeMillis());
		User user = (User) request.getSession().getAttribute("user");
		int count = 0;
		try {
			user = userService.getUserById(user.getUserId());
			if (user.getUserPassword().equals(MD5Utils.MD5(oldPassword))) {
				user.setUserPassword(MD5Utils.MD5(newPassword));
				count = userService.updateUser(user);
			} else {
				count = -1;
			}
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			return new JSONPObject(jsonpCallback, count);
		} catch (Exception e) {
			count = -1;
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			return new JSONPObject(jsonpCallback, count);
		}
	}

	@RequestMapping("update")
	@ResponseBody
	public JSONPObject updata(
			@RequestParam("jsonpCallback") String jsonpCallback,
			HttpServletRequest request, HttpServletResponse response,
			String userName, String userEmail, String userAbout) {
		logger.info("------star------" + request.getPathInfo()
				+ "------time------" + System.currentTimeMillis());
		User user = (User) request.getSession().getAttribute("user");
		int count = 0;
		try {
			user = userService.getUserById(user.getUserId());
			user.setUserAbout(userAbout);
			user.setUserEmail(userEmail);
			user.setUserName(userName);
			count = userService.updateUser(user);
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			return new JSONPObject(jsonpCallback, count);
		} catch (Exception e) {
			count = -1;
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			return new JSONPObject(jsonpCallback, count);
		}

	}

	@RequestMapping("getUser")
	@ResponseBody
	public JSONPObject getUser(
			@RequestParam("jsonpCallback") String jsonpCallback,
			HttpServletRequest request, HttpServletResponse response) {
		logger.info("------star------" + request.getPathInfo()
				+ "------time------" + System.currentTimeMillis());
		User user = (User) request.getSession().getAttribute("user");
		try {
			user = userService.getUserById(user.getUserId());
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			return new JSONPObject(jsonpCallback, user);
		} catch (Exception e) {
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			return new JSONPObject(jsonpCallback, user);
		}

	}

	@RequestMapping("login")
	@ResponseBody
	public JSONPObject login(
			@RequestParam("jsonpCallback") String jsonpCallback,
			HttpServletRequest request, HttpServletResponse response, int id,
			String password) {
		logger.info("------end------" + request.getPathInfo()
				+ "------time------" + System.currentTimeMillis());
		User user = userService.getUserById(id);
		int count = 0;
		if (user == null) {
			count = -1;
			JSONPObject jsonp = new JSONPObject(jsonpCallback, count);
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			return jsonp;
		}
		if (!user.getUserPassword().equals(MD5Utils.MD5(password))) {
			count = -2;
			JSONPObject jsonp = new JSONPObject(jsonpCallback, count);
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			return jsonp;
		}
		HttpSession httpSession = request.getSession();
		httpSession.setAttribute("user", user);
		JSONPObject jsonp = new JSONPObject(jsonpCallback, user);
		logger.info("------end------" + request.getPathInfo()
				+ "------time------" + System.currentTimeMillis());
		return jsonp;
	}
}
